<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title></title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="memcache.examples.html">范例</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="class.memcache.html">Memcache</a></div>
 <div class="up"><a href="memcache.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="memcache.examples-overview" class="section">
  <p class="para">
   <div class="example" id="example-4313">
    <p><strong>Example #1 memcache扩展概览示例</strong></p>
    <div class="example-contents"><p>
    在这个例子中，一个对象被保存到缓存中，又被取回。对象和其他非标量类型在存储
    之前会先被序列化，因此它不可以用来存储资源类型（连接标记及其他）。（译注：请注意隐式的
    资源类型存储，比如要保存的对象自身不是资源，但其中某个属性却是资源）
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$memcache&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Memcache</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$memcache</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">11211</span><span style="color: #007700">)&nbsp;or&nbsp;die&nbsp;(</span><span style="color: #DD0000">"Could&nbsp;not&nbsp;connect"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$version&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$memcache</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getVersion</span><span style="color: #007700">();<br />echo&nbsp;</span><span style="color: #DD0000">"服务端版本信息:&nbsp;"</span><span style="color: #007700">.</span><span style="color: #0000BB">$version</span><span style="color: #007700">.</span><span style="color: #DD0000">"&lt;br/&gt;\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$tmp_object&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">stdClass</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$tmp_object</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">str_attr&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'test'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$tmp_object</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">int_attr&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">123</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$memcache</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">set</span><span style="color: #007700">(</span><span style="color: #DD0000">'key'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$tmp_object</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">)&nbsp;or&nbsp;die&nbsp;(</span><span style="color: #DD0000">"Failed&nbsp;to&nbsp;save&nbsp;data&nbsp;at&nbsp;the&nbsp;server"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"将数据保存到缓存中（数据10秒后失效）&lt;br/&gt;\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$get_result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$memcache</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">get</span><span style="color: #007700">(</span><span style="color: #DD0000">'key'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"从缓存中取到的数据:&lt;br/&gt;\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$get_result</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4314">
    <p><strong>Example #2 使用memcache的session处理器</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$session_save_path&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"tcp://</span><span style="color: #0000BB">$host</span><span style="color: #DD0000">:</span><span style="color: #0000BB">$port</span><span style="color: #DD0000">?persistent=1&amp;weight=2&amp;timeout=2&amp;retry_interval=10,&nbsp;&nbsp;,tcp://</span><span style="color: #0000BB">$host</span><span style="color: #DD0000">:</span><span style="color: #0000BB">$port</span><span style="color: #DD0000">&nbsp;&nbsp;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'session.save_handler'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'memcache'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'session.save_path'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$session_save_path</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="memcache.examples.html">范例</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="class.memcache.html">Memcache</a></div>
 <div class="up"><a href="memcache.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
